// miniprogram/pages/order/index.js
import * as R from 'ramda';
import U from '../../utils/index';
import config from '../../config/index'
// 人工智能判断搜索输入的是什么
const judgeSearchVal = R.cond([
  [ v => /^1([358][0-9]|4[579]|66|7[0135678]|9[89])\d{8}$/.test(v), v => ({ mobile: v }) ],
  [ v => escape(v).indexOf( "%u" ) < 0, v => ({}) ],
  [ R.T, v => ({ realname: v }) ]
])

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderTypes: config.orderTypes,
    currentPageData: {
      pageData: [],
      pageNum: 1,
      isLastPage: false,
      isShowPageLoading: false,
    },
    initialData: null,
    valueSearch: null,
    // 请求列表的参数
    paramSearch: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 设置页面初始数据
    this.setData({
      initialData: R.clone(this.data.currentPageData)
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    R.compose(
      () => this.turnPage(),
      () => this.initializeData()
    )();
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.turnPage()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 初始化页面数据
  initializeData() {
    this.setData({
      currentPageData: R.clone(this.data.initialData)
    })
  },
  turnPage() {
    U.turnPage(() => U.reqPost({
      url:'/order/get_list',
      data: {
        ...this.data.paramSearch,
        page_num: this.data.currentPageData.pageNum,
        page_size: 5
      }
    })).run(this);
    return;
  },
  onChangeSearch(e) {
    this.setData({
      valueSearch: e.detail
    });
  },
  onSearch(e) {
    R.compose(
      () => this.turnPage(),
      () => this.initializeData(),
      paramSearch => this.setData({ paramSearch }),
      judgeSearchVal,
      U.trim
    )(this.data.valueSearch);
  }
})
